<template>
    <div>
        <ul class="myMods">
            <li v-for="mod in myModules" :key="mod.path" :style="{background: '#909399'}">
                <a v-if="mod.path.isLink()" :href="mod.path" target="_blank">
                    <el-icon>
                        <component :is="mod.meta?.icon||'el-icon-menu'"/>
                    </el-icon>
                    <p>{{ mod.meta?.title }}</p>
                </a>
                <router-link v-else :to="{ path: mod.path }">
                    <el-icon>
                        <component :is="mod.meta?.icon||'el-icon-menu'"/>
                    </el-icon>
                    <p>{{ mod.meta?.title }}</p>
                </router-link>
            </li>
            <li class="modItem-add" @click="show">
                <a href="javascript:void(0)">
                    <el-icon>
                        <svg-icon icon="icon-plus"/>
                    </el-icon>
                </a>
            </li>
        </ul>

        <el-drawer v-model="modsDrawer" :size="570" destroy-on-close title="添加应用">
            <div class="setMods">
                <h4>我的常用 ( {{ myModules.length }} )</h4>
                <draggable v-model="myModules" animation="200" group="people" item-key="path" tag="ul">
                    <template #item="{ element }">
                        <li :style="{background:element.meta.color||'#909399'}">
                            <el-icon>
                                <component :is="element.meta.icon||'el-icon-menu'"/>
                            </el-icon>
                            <p>{{ element.meta.title }}</p>
                        </li>
                    </template>
                </draggable>
            </div>
            <div class="setMods">
                <h4>全部应用 ( {{ filterModules.length }} )</h4>
                <draggable v-model="filterModules" :sort="false" animation="200" group="people" item-key="path" tag="ul">
                    <template #item="{ element }">
                        <li :style="{background:element.meta.color||'#909399'}">
                            <el-icon>
                                <component :is="element.meta.icon||'el-icon-menu'"/>
                            </el-icon>
                            <p>{{ element.meta.title }}</p>
                        </li>
                    </template>
                </draggable>
            </div>
            <template #footer>
                <el-button @click="modsDrawer=false">取消</el-button>
                <el-button type="primary" @click="saveMods">保存</el-button>
            </template>
        </el-drawer>
    </div>
</template>

<script lang="ts" setup>
import {onMounted, ref} from 'vue'
import draggable from 'vuedraggable'
import {Storage} from '@/utils'
import store from '@/store'
import {ElMessage} from "element-plus";



const myModuleNames = ref<string[]>([]);
const myModules = ref<DynamicRouteRecordRaw[]>([]);
const filterModules = ref<DynamicRouteRecordRaw[]>([]);
const modsDrawer = ref(false);

onMounted(() => {
    myModuleNames.value = Storage.getItem("my-mods") || []
    Array.from(store.userStore.menus).flatten().forEach((item: any) => {
        if (item.meta.hidden || (item.children && item.children.length > 0)) {
            return false
        } else {
            if (myModuleNames.value.includes(item.name)) {
                myModules.value.push(item)
            } else {
                filterModules.value.push(item)
            }
        }
    })
});

const show = () => {
    modsDrawer.value = true
}

const saveMods = () => {
    const myModsName = myModules.value.map((v:any) => v.name)
    Storage.setItem("my-mods", myModsName)
    ElMessage.success("设置常用成功")
    modsDrawer.value = false
}
</script>

<style scoped>
.myMods {
    list-style: none;
    margin: -10px;
}

.myMods li {
    display: inline-block;
    width: 100px;
    height: 100px;
    vertical-align: top;
    transition: all 0.3s ease;
    margin: 10px;
    border-radius: 5px;
}

.myMods li:hover {
    opacity: 0.8;
}

.myMods li a {
    width: 100%;
    height: 100%;
    padding: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: #fff;
}

.myMods li i {
    font-size: 26px;
    color: #fff;
}

.myMods li p {
    font-size: 12px;
    color: #fff;
    margin-top: 10px;
    width: 100%;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}

.modItem-add {
    border: 1px dashed #ddd;
    cursor: pointer;
}

.modItem-add i {
    font-size: 30px;
    color: #999 !important;
}

.modItem-add:hover, .modItem-add:hover i {
    border-color: #409eff;
    color: #409eff !important;
}

.setMods {
    padding: 0 20px;
}

.setMods h4 {
    font-size: 14px;
    font-weight: normal;
}

.setMods ul {
    margin: 20px -5px;
    min-height: 90px;
}

.setMods li {
    display: inline-block;
    width: 80px;
    height: 80px;
    text-align: center;
    margin: 5px;
    color: #fff;
    vertical-align: top;
    padding: 4px;
    padding-top: 15px;
    cursor: move;
    border-radius: 3px;
}

.setMods li i {
    font-size: 20px;
}

.setMods li p {
    font-size: 12px;
    margin-top: 10px;
}

.setMods li.sortable-ghost {
    opacity: 0.3;
}
</style>
